package date;


import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Month;
import java.time.Year;
import java.time.YearMonth;
import java.util.Calendar;

public class Ji {

    public static void main(String[] args) {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 获取当前日期
        Calendar calendar = Calendar.getInstance();

        // 获取当前季度
        int currentQuarter = (calendar.get(Calendar.MONTH) / 3) + 1;
        System.out.println(currentQuarter);

        // 获取当前季度的第一天和最后一天
        Calendar firstDayOfCurrentQuarter = Calendar.getInstance();
        firstDayOfCurrentQuarter.set(Calendar.MONTH, (currentQuarter - 1) * 3);
        firstDayOfCurrentQuarter.set(Calendar.DAY_OF_MONTH, 1);
        setToBeginningOfDay(firstDayOfCurrentQuarter);

        Calendar lastDayOfCurrentQuarter = Calendar.getInstance();
        lastDayOfCurrentQuarter.set(Calendar.MONTH, currentQuarter * 3);
        lastDayOfCurrentQuarter.set(Calendar.DAY_OF_MONTH, 1);
        lastDayOfCurrentQuarter.add(Calendar.DATE, -1);
        setToEndOfDay(lastDayOfCurrentQuarter);

        System.out.println("季度：" + firstDayOfCurrentQuarter.get(Calendar.YEAR) + getQuarter(firstDayOfCurrentQuarter.get(Calendar.MONTH) + 1));
        System.out.println("第一天：" + simpleDateFormat.format(firstDayOfCurrentQuarter.getTime()));
        System.out.println("最后一天：" + simpleDateFormat.format(lastDayOfCurrentQuarter.getTime()));

        // 获取前三个季度的第一天和最后一天
        for (int i = 0; i < 3; i++) {
            firstDayOfCurrentQuarter.add(Calendar.MONTH, -3);
            setToBeginningOfDay(firstDayOfCurrentQuarter);

            lastDayOfCurrentQuarter.add(Calendar.MONTH, -3);
            setToEndOfDay(lastDayOfCurrentQuarter);
            System.out.println("季度：" + firstDayOfCurrentQuarter.get(Calendar.YEAR) + getQuarter(firstDayOfCurrentQuarter.get(Calendar.MONTH) + 1));
            System.out.println("第一天：" + simpleDateFormat.format(firstDayOfCurrentQuarter.getTime()));
            System.out.println("最后一天：" + simpleDateFormat.format(lastDayOfCurrentQuarter.getTime()));
        }
    }

    private static void setToBeginningOfDay(Calendar calendar) {
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
    }

    private static void setToEndOfDay(Calendar calendar) {
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
    }

    private static String getQuarter(int month) {
        if (month < 1 || month > 12) {
            return "月份无效";
        } else if (month >= 1 && month <= 3) {
            return "第一季度";
        } else if (month >= 4 && month <= 6) {
            return "第二季度";
        } else if (month >= 7 && month <= 9) {
            return "第三季度";
        } else {
            return "第四季度";
        }
    }
}
